home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Apache 1.0 / src / mod_dld.c < prev    next >
Text File  |  1995-12-04  |  6KB  |  192 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54.  
  55. /*
  56.  * A first stab at dynamic loading, using the GNU dld library
  57.  * (or at least, an embarassingly old version of it...).
  58.  */
  59.  
  60. #include "httpd.h"
  61. #include "http_config.h"
  62. #include "http_conf_globals.h"    /* server_argv0.  Sigh... */
  63. #include <dld.h>
  64.  
  65. /*
  66.  * The hard part of implementing LoadModule is deciding what to do about
  67.  * rereading the config files.  This proof-of-concept implementation takes the 
  68.  * cheap way out:  we only actually load the modules the first time through.
  69.  */
  70.  
  71. static int been_there_done_that = 0; /* Loaded the modules yet? */
  72. static int have_symbol_table = 0;
  73.  
  74. char *insure_dld_sane()
  75. {
  76.     int errcode;
  77.     char *bin_name;
  78.     
  79.     if (have_symbol_table) return NULL;
  80.  
  81.     bin_name = dld_find_executable (server_argv0);
  82.     
  83.     if ((errcode = dld_init (bin_name))) {
  84.     dld_perror (server_argv0);
  85.     return "Cannot find server binary (needed for dynamic linking).";
  86.     }
  87.  
  88.     have_symbol_table = 1;
  89.     return NULL;
  90. }
  91.  
  92. char *link_file (pool *p, char *filename)
  93. {
  94.     int errcode;
  95.     
  96.     filename = server_root_relative (p, filename);
  97.     if ((errcode = dld_link (filename))) {
  98.     dld_perror (server_argv0);
  99.     return pstrcat (p, "Cannot load ", filename, " into server", NULL);
  100.     }
  101.     return NULL;
  102. }
  103.  
  104. char *load_module (cmd_parms *cmd, void *dummy, char *modname, char *filename)
  105. {
  106.     char *errname;
  107.     module *modp;
  108.  
  109.     if (been_there_done_that) return NULL;
  110.     
  111.     if ((errname = insure_dld_sane())) return errname;
  112.     if ((errname = link_file (cmd->pool, filename))) return errname;
  113.     if (!(modp = (module *)dld_get_symbol (modname))) {
  114.     return pstrcat (cmd->pool, "Can't find module ", modname,
  115.                        " in file ", filename, NULL);
  116.     }
  117.  
  118.     add_module (modp);
  119.  
  120.     /* Alethea Patch (rws,djw2) - need to run configuration functions
  121.        in new modules */
  122.  
  123.     if (modp->create_server_config)
  124.       ((void**)cmd->server->module_config)[modp->module_index]=
  125.     (*modp->create_server_config)(cmd->pool, cmd->server);
  126.  
  127.     if (modp->create_dir_config)
  128.       ((void**)cmd->server->lookup_defaults)[modp->module_index]=
  129.     (*modp->create_dir_config)(cmd->pool, NULL);
  130.  
  131.  
  132.     return NULL;
  133. }
  134.  
  135. char *load_file (cmd_parms *cmd, void *dummy, char *filename)
  136. {
  137.     char *errname;
  138.     
  139.     if (been_there_done_that) return NULL;
  140.     
  141.     if ((errname = insure_dld_sane())) return errname;
  142.     if ((errname = link_file (cmd->pool, filename))) return errname;
  143.     return NULL;
  144. }
  145.  
  146. void check_loaded_modules (server_rec *dummy, pool *p)
  147. {
  148.     if (been_there_done_that) return;
  149.  
  150.     if (dld_undefined_sym_count > 0) {
  151.     /* Screwup.  Do the best we can to inform the user, and exit */
  152.     char **bad_syms = dld_list_undefined_sym();
  153.     int i;
  154.  
  155.     fprintf(stderr, "Dynamic linking error --- symbols left undefined.\n");
  156.     fprintf(stderr, "(It may help to relink libraries).\n");
  157.     fprintf(stderr, "Undefined symbols follow:\n");
  158.     
  159.     for (i = 0; i < dld_undefined_sym_count; ++i)
  160.         fprintf (stderr, "%s\n", bad_syms[i]);
  161.  
  162.     exit (1);
  163.     }
  164.     
  165.     been_there_done_that = 1;
  166. }
  167.  
  168. command_rec dld_cmds[] = {
  169. { "LoadModule", load_module, NULL, RSRC_CONF, TAKE2,
  170.   "a module name, and the name of a file to load it from"},
  171. { "LoadFile", load_file, NULL, RSRC_CONF, ITERATE,
  172.   "files or libraries to link into the server at runtime"},
  173. { NULL }
  174. };
  175.  
  176. module dld_module = {
  177.    STANDARD_MODULE_STUFF,
  178.    check_loaded_modules,    /* initializer */
  179.    NULL,            /* create per-dir config */
  180.    NULL,            /* merge per-dir config */
  181.    NULL,            /* server config */
  182.    NULL,            /* merge server config */
  183.    dld_cmds,            /* command table */
  184.    NULL,            /* handlers */
  185.    NULL,            /* filename translation */
  186.    NULL,            /* check_user_id */
  187.    NULL,            /* check auth */
  188.    NULL,            /* check access */
  189.    NULL,            /* type_checker */
  190.    NULL                /* logger */
  191. };
  192.